//var storage = plus.storage;
var server_url = "http://10.10.2.27";
//var server_url = "http://10.10.12.53:8080";
//var server_url = "http://10.200.107.102:8080";
var url_tail = '&format=json&callback=?';

var isOnLine = true;
function UpdateState () {
	if (navigator.onLine) {
		isOnLine = true;
	} else {
		isOnLine = false;
	}
}
window.ononline = function () {
    isOnLine = true;
}
window.onoffline = function () {
    isOnLine = false;
}

function getData (item) {
	var storage = plus.storage;
	var data = JSON.parse(storage.getItem(item));
	if (data != null) {
		return data;
	} else{
		return false;
	}
}

//清除缓存，目的是防止应用闪退
function setItem(key,value){
	var storage = plus.storage;
	try{
		storage.setItem(key,value);
	    return true;
	}catch(e){
	   if(e.name == 'QuotaExceededError') {
	       clear();
	       plus.nativeUI.alert("缓存已满，已自动清除缓存数据，请重新登录!");
	       window.location.href = "login.html";
	       return false;
        }
	}
}


function setData (item, data) {
	var storage = plus.storage;
	try{
		storage.setItem(item, JSON.stringify(data));
	    return true;
	}catch(e){
	   if(e.name == 'QuotaExceededError') {
	       clear();
	       plus.nativeUI.alert("缓存已满，已自动清除缓存数据，请重新登录!");
	       window.location.href = "login.html";
	       return false;
        }
	}
}

function showLogList(id){
	var storage = plus.storage;
	var log_list = [];
	for(var i=0; i<storage.getLength(); i++){
		if (storage.key(i).indexOf("LogList"+id) == 0) {
			log_list[log_list.length] = storage.key(i);
		}
	}
	return log_list;
}

function showAllItems(){
	var storage = plus.storage;
	for(var i=0; i<storage.getLength(); i++){
		console.log(storage.key(i));
	}
}

function showLogListMax(id){
	var storage = plus.storage;
	var log_list = showLogList(id);
	var list = [];
	for (var i=0; i<log_list.length; i++) {
		list[i] = parseInt(log_list[i].replace("LogList"+id,""));
	}
	var max = 0;
	for (var i=0; i<list.length; i++) {
		if (max < list[i]) {
			max = list[i];
		}
	}
	return max;
}

function setUploaded (item) {
	var storage = plus.storage;
	var data = JSON.parse(storage.getItem(item));
	data.uploaded = true;
	storage.removeItem(item);
	setItem(item, JSON.stringify(data));
	return true;
}

function removeItem (item) {
	var storage = plus.storage;
	storage.removeItem(item);
	return true;
}

function clear () {
	var storage = plus.storage;
	storage.clear();
}

function date_formate (date) {
	var formate = "";
	formate = date.getFullYear()+"-";
	if(date.getMonth()+1 < 10){
		formate = formate + "0" + (date.getMonth()+1)+"-";
	}else{
		formate = formate + (date.getMonth()+1)+"-";
	}
	if(date.getDate() < 10){
		formate = formate + "0" + date.getDate();
	}else{
		formate = formate + date.getDate();
	}
	return formate;
}
function time_formate (date) {
	var formate = "";
	if(date.getHours() < 10){
		formate = formate + "0" + (date.getHours())+":";
	}else{
		formate = formate + (date.getHours())+":";
	}
	if(date.getMinutes() < 10){
		formate = formate + "0" + date.getMinutes();
	}else{
		formate = formate + date.getMinutes();
	}
	return formate;
}